package com.xy.blog.user.dao.impl;

import com.github.pagehelper.PageHelper;
import com.xy.blog.core.dao.IBaseCommonDao;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.controller.model.req.query.UserTemplateQuery;
import com.xy.blog.user.dao.IUserTemplateAttrDictRelDao;
import com.xy.blog.user.dao.IUserTemplateDao;
import com.xy.blog.user.dao.mapper.UserTemplateMapper;
import com.xy.blog.user.dao.mapper.rel.UserTemplateAttrDictRelMapper;
import com.xy.blog.user.dao.model.UserTemplateDO;
import com.xy.blog.user.dao.model.rel.UserTemplateAttrRelDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/4
 */

@Repository
public class UserTemplateDaoImpl implements IBaseCommonDao<UserTemplateDO, UserTemplateQuery> , IUserTemplateDao , IUserTemplateAttrDictRelDao {

    @Autowired
    private UserTemplateMapper userTemplateMapper;

    @Autowired
    private UserTemplateAttrDictRelMapper userTemplateAttrDictRelMapper;


    @Override
    public PageResp<UserTemplateDO> listByPage(PageReq<UserTemplateQuery> req) {
        PageHelper.startPage(req);
        List<UserTemplateDO> userTemplateDOS = userTemplateMapper.selectAll();
        return PageResp.data(userTemplateDOS);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(UserTemplateDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            item.setCreateTime(LocalDateTime.now());
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(LocalDateTime.now());
        }
        userTemplateMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(UserTemplateDO item) {
        userTemplateMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        userTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UserTemplateDO getById(Long id) {
        return userTemplateMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserTemplateDO> select(UserTemplateDO item) {
        Example example = new Example(UserTemplateDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(item.getName())) {
            criteria.andLike("name", "%" + item.getName().trim() + "%");
        }
        return userTemplateMapper.selectByExample(example);
    }

    @Override
    public int selectCount(UserTemplateDO item) {
        return userTemplateMapper.selectCount(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void saveRel(UserTemplateAttrRelDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            item.setCreateTime(LocalDateTime.now());
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(LocalDateTime.now());
        }
        userTemplateAttrDictRelMapper.insert(item);
    }

    /**
     * 根据用户模板删除关联关系
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteRelByTemplateId(Long id) {
        UserTemplateAttrRelDO userTemplateAttrRelDO = new UserTemplateAttrRelDO();
        userTemplateAttrRelDO.setUserTemplateId(id);
        userTemplateAttrDictRelMapper.delete(userTemplateAttrRelDO);
    }

    /**
     * 根据属性id删除关联的属性
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteRelById(Long id) {
        userTemplateAttrDictRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UserTemplateAttrRelDO> selectRel(UserTemplateAttrRelDO userTemplateAttrRelDO) {
        return userTemplateAttrDictRelMapper.select(userTemplateAttrRelDO);
    }
}
